#include "frm_profile.h"
#include "ui_frm_profile.h"
#include "frm_menu.h"
#include <QDesktopWidget>
#include <QTextStream>
#include <QString>
#include <QMessageBox>
#include <QVector>
#include <cstdio>

frm_profile::frm_profile(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::frm_profile),profiledata("profile.txt")
{
    ui->setupUi(this);
    setWindowFlags( Qt:: WindowTitleHint | Qt:: CustomizeWindowHint | Qt:: WindowMinimizeButtonHint | Qt:: WindowMaximizeButtonHint);
    this->move(QApplication::desktop()->screen()->rect().center() - this->rect().center());
    if(!profiledata.open(QIODevice::ReadWrite))
        qFatal("No se pudo abrir el fichero profile.txt");
    QTextStream in(&profiledata);
    QString regist = in.readLine();
    if(regist.toInt()){
        ui->textName->setText(in.readLine());
        ui->textSurname->setText(in.readLine());
        ui->textHeight->setText(in.readLine());
        ui->textWeight->setText(in.readLine());
        QString sex = in.readLine();
        if(sex == "")
            ui->boxSex->setCurrentIndex(0);
        if(sex == "Man")
           ui->boxSex->setCurrentIndex(1);
        else{
            if(sex == "Woman")
                ui->boxSex->setCurrentIndex(2);
            else
                ui->boxSex->setCurrentIndex(0);
        }
        qint32 y = in.readLine().toInt();
        qint32 m = in.readLine().toInt();
        qint32 d = in.readLine().toInt();
        ui->datetext->setDate(QDate(y,m,d));
    }
}

frm_profile::~frm_profile()
{
    delete ui;
}

void frm_profile::on_btn_save_clicked()
{
    profiledata.reset();
    QVector<QString> memorydata(6);
    QMessageBox aviso;
    if(ui->textName->text()==""){
        aviso.setText("The name is missing");
        aviso.exec();
        return;
    }
    else
        memorydata[0] = ui->textName->text();
    if(ui->textSurname->text()==""){
        aviso.setText("The surname is missing");
        aviso.exec();
        return;
    }
    else
        memorydata[1] = ui->textSurname->text();
    if(ui->textHeight->text()==""){
        aviso.setText("The height is missing");
        aviso.exec();
        return;
    }
    else
        memorydata[2] = ui->textHeight->text();
    if(ui->textWeight->text()==""){
        aviso.setText("The weight is missing");
        aviso.exec();
        return;
    }
    else
        memorydata[3] = ui->textWeight->text();
    if(ui->boxSex->currentText() == ""){
        aviso.setText("The sex is missing");
        aviso.exec();
        return;
    }
    else
        memorydata[4] = ui->boxSex->currentText();
    QDate temporalDate = ui->datetext->date();
    char datestring[10];
    sprintf(datestring,"%d\n%d\n%d", temporalDate.year(), temporalDate.month(), temporalDate.day());
    memorydata[5] = datestring;

    QTextStream out(&profiledata);
    out << 1 << endl;
    foreach(const QString& elemen, memorydata){
        out << elemen << endl;
    }

    profiledata.close();
    frm_menu *menu = new frm_menu();
    this->hide();
    menu->show();
}

void frm_profile::on_btn_menu_clicked()
{
    profiledata.close();
    frm_menu *menu = new frm_menu();
    this->hide();
    menu->show();
}
